package vn.cuhanh.handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import vn.cuhanh.bean.Hero;
import android.util.Log;

public class HeroesReaderHandler extends DefaultHandler {

	private final static String TAG_ITEM = "Hero";
    private final static String[] xmltags = { "id", "name", "avatar", "rated", "type", "nation", "property", "forte", "loved", "lovedRated", "skillInfo", "detailInfo", "suggestedItem", "otherName", "avatarUrl", "fullInfoWebUrl" };

    private Hero currentitem = null;
    private ArrayList<Hero> itemarray = null;
    private int currentindex = -1;
    private boolean isParsing = false;
    private StringBuilder builder = new StringBuilder();
     
    public HeroesReaderHandler(ArrayList<Hero> itemarray) {
            super();
             
            this.itemarray = itemarray;
    }
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		
		if(isParsing && -1 != currentindex && null != builder)
        {
                builder.append(ch,start,length);
        }
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		
//		Log.d("handler", "currentindex: " + currentindex + " " + (builder != null ? builder.toString() : "null"));
		if(localName.equalsIgnoreCase(TAG_ITEM))
        {
                isParsing = false;
        }
        else if(currentindex != -1)
        {
                if(isParsing)
                {
                        switch(currentindex)
                        {
                                case 0: currentitem.setId(Integer.parseInt(builder.toString()));                 break; 
                                case 1: currentitem.setName(builder.toString());                  break;
                                case 2: currentitem.setAvatar(builder.toString());                  break;
                                case 3: currentitem.setRate(Integer.parseInt(builder.toString()));    break;
                                case 4: currentitem.setType(Integer.parseInt(builder.toString()));                 break; 
                                case 5: currentitem.setNation(Integer.parseInt(builder.toString()));                  break;
                                case 6: currentitem.setProperty(Integer.parseInt(builder.toString()));                  break;
                                case 7: currentitem.setForte(Integer.parseInt(builder.toString()));    break;
                                case 8: currentitem.setLoved(Integer.parseInt(builder.toString()));                 break; 
                                case 9: currentitem.setLovedRate(Integer.parseInt(builder.toString()));                  break;
                                case 10: currentitem.setSkillInfo(builder.toString());                  break;
                                case 11: currentitem.setDetailInfo(builder.toString());    break;
                                case 12: currentitem.setSuggestedItems(builder.toString());    break;
                                case 13: currentitem.setOtherName(builder.toString());    break;
                                case 14: currentitem.setAvatarUrl(builder.toString());    break;
                                case 15: currentitem.setFullInfoWebUrl(builder.toString());    break;
                        }
                }
        }
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if(localName.equalsIgnoreCase(TAG_ITEM))
        {
//			Log.d("handler", "localName: " + localName + "equals");
                currentitem = new Hero();
                currentindex = -1;
                isParsing = true;
                 
                itemarray.add(currentitem);
        } else {
                currentindex = itemIndexFromString(localName);
                 
                builder = null;
                 
                if(-1 != currentindex)
                        builder = new StringBuilder();
//            Log.d("handler", "localName: " + localName + "not equals. Current index" + currentindex);
        }
	}
	
	private int itemIndexFromString(String tagname){
        int itemindex = -1;

        for(int index= 0; index<xmltags.length; ++index)
        {
                if(tagname.equalsIgnoreCase(xmltags[index]))
                {
                        itemindex = index;
                         
                        break;
                }
        }
         
        return itemindex;
	}
	
}
